#!/bin/sh

set -ex

package_name_transform_from_uppm_to_apk() {
    case $1 in
        gm4)      printf '%s\n' 'm4'        ;;
        gtar)     printf '%s\n' 'tar'       ;;
        gsed)     printf '%s\n' 'sed'       ;;
        gmake)    printf '%s\n' 'make'      ;;
        gindent)  printf '%s\n' 'indent'    ;;
        golang)   printf '%s\n' 'go'        ;;
        python3)  printf '%s\n' 'python3 python3-dev' ;;
        xsltproc) printf '%s\n' 'libxslt'   ;;
        bsdtar)   printf '%s\n' 'libarchive-tools' ;;
        pkg-config) printf '%s\n' 'pkgconf' ;;
        glib-tools) printf '%s\n' 'glib'    ;;
        gettext-tools) printf '%s\n' 'gettext gettext-dev' ;;
        cabal-install) printf '%s\n' 'cabal'   ;;
        perl)     printf '%s\n' 'perl perl-doc' ;;
        *)        printf '%s\n' "$1"
    esac
}

main() {
    case $1 in
        about)
            apk --version
            ;;
        update)
            apk update
            ;;
        install)
            shift 1
            for UPPM_PACKAGE_NAME in "$@"
            do
                APK_PACKAGE_NAME="$(package_name_transform_from_uppm_to_apk "$UPPM_PACKAGE_NAME")"
                apk add $APK_PACKAGE_NAME
            done
            ;;
        is-installed)
            shift 1
            APK_PACKAGE_NAME="$(package_name_transform_from_uppm_to_apk "$1")"
            apk info "$APK_PACKAGE_NAME" 2>/dev/null
            ;;
        info)
            shift 1
            case $2 in
                installed-dir)
                    printf '%s\n' '/usr'
                    ;;
            esac
            ;;
        *)  apk "$@"
    esac
}

main "$@"
